Page({

  /**
   * 页面的初始数据
   */
  data: {
    current: 1,
    codeText: '获取验证码',
    counting: false,
    username: '',
    phone: '',
    password: '',
    email: '',
    captcha: '',
    checkPassword: ''
  },
  // 登陆注册页面转换监听
  click(e) {
    let index = e.currentTarget.dataset.code;
    this.setData({
      current: index
    })
  },
  // 登录事件
  loginFormSubmit(e) {
    var that = this
    that.data.username = e.detail.value.username
    that.data.password = e.detail.value.password
    wx.request({
      url: 'http://localhost:8081/api/user/login',
      data: {
        username: e.detail.value.username,
        password: e.detail.value.password
      },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        wx.showToast({
          title: res.data.message,
          icon: res.data.message == "登录成功" ? "success" : "error",
          duration: 2000
        })
        if (res.data.code == 20000 && res.data.message == "登录成功") {
          wx.setStorageSync('token', res.data.result)
          wx.setStorageSync('username', that.data.username)
          setTimeout(function () {
            wx.switchTab({
              url: '../mine/mine',
            })
          }, 1000)
        }
      }
    })
  },
  // 注册事件
  registerFormSubmit(e) {
    var that = this

    that.data.phone = e.detail.value.phone
    that.data.email = e.detail.value.email
    that.data.captcha = e.detail.value.captcha
    that.data.password = e.detail.value.password
    that.data.checkPassword = e.detail.value.checkPassword
    console.log(e.detail.value.email)
    if (that.data.phone != '' && that.data.email != '' && that.data.captcha != '' &&
      that.data.password != '' && that.data.checkPassword != '') {
      if (that.data.password != that.data.checkPassword) {
        wx.showToast({
          title: '密码不一致',
          icon: 'error'
        })
        return
      } else {
        wx.request({
          url: 'http://localhost:8081/api/user/register/' + that.data.captcha,
          method: 'POST',
          data: {
            username: that.data.phone,
            phone: that.data.phone,
            email: that.data.email,
            password: that.data.password
          },
          success:function(res){
            if (res.data.code == 10009) {
              wx.showToast({
                title: '注册成功',
              })
              setTimeout(function () {
                that.setData({
                  current: 1
                })
              }, 1000)
            }
          }
        })
      }
    }else{
      wx.showToast({
        title: '数据不完整',
        icon: 'error'
      })
    }


  },
  //获取验证码 
  getCode(e) {
    console.log(e)
    var that = this;
    if (!that.data.counting) {
      wx.request({
        url: 'http://localhost:8081/api/user/getCheckNumber',
        method: 'GET'
      })
      wx.showToast({
        title: '验证码已发送',
      })
      //开始倒计时60秒
      that.countDown(that, 60);
    }
  },
  //倒计时60秒
  countDown(that, count) {
    if (count == 0) {
      that.setData({
        codeText: '获取验证码',
        counting: false
      })
      return;
    }
    that.setData({
      counting: true,
      codeText: count + '秒后重新获取',
    })
    setTimeout(function () {
      count--;
      that.countDown(that, count);
    }, 1000);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})